layui.use(['form'], function () {
    var form = layui.form;
    var $ = layui.$;
    var layer = layui.layer;

    form.verify({
      required: function (value, item) {
        if (!value) {
          return '该项为必填项';
        }
      }
    });

    form.on('submit(login)', function (data) {
      var username = data.field.username;
      var password = data.field.password;

      axios.post('http://localhost:9000/users/login', {
        username: username,
        password: password
      })
        .then(function (response) {
          if (response.data.code === 1) {
            layer.msg(response.data.message, { icon: 1 });
            var userData = response.data.user;
            ['nickname', 'id'].forEach(function (key) {
              layui.data('shop', {
                key: key,
                value: userData[key]
              });
            });
            layui.data('shop',{
                key:'token',
                value:response.data.token
            })
            location.href = './index.html'
          } else {
            layer.msg(response.data.message, { icon: 0 })
          }
        })

      return false; 
    });
  });